<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>角色管理</title>

    <#include "../common/link.ftl">

    <script>
        //左移右移
        function moveAll(srcClass, targetClass) {
            //找到左边框
            var $srcClass1 = $("." + srcClass + " option");
            //找到右边框,把左边框的值追加过去
            $("." + targetClass).append($srcClass1);
        }

        //左移右移,选中的才移动
        function moveSelected(srcClass, targetClass) {
            //找到左边框
            var $srcClass1 = $("." + srcClass + " option:selected");
            //找到右边框,把左边框的值追加过去
            $("." + targetClass).append($srcClass1);
        }


        //当页面加载完
        $(function () {
            //提交表单操作,根据id找到btn-submit
            $("#btn-submit").click(function () {
                //右边框选中的才让他提交表单
                $(".selfPermissions option").prop("selected", true);
                //提交表单
                $("#editForm").submit();
            });

            //角色去重,当页面加载完
            //先找到右边框
            var $value2 = $(".selfPermissions option").map(function (index, value) {
                //console.log(value);
                return $(value).val();
            });
            console.log($value2.get());
            //在找到左边框
            $(".allPermissions option").each(function (index, value) {
                //console.log(value);
                var $value1 = $(value).val();
                //判断,第一个参数是左边框的value值,第二个参数是存储右边框value值的数组
                if ($.inArray($value1, $value2) != -1) {
                    //由左边框删除他那里重复的value值
                    $(value).remove();
                }
            });
        });
    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <#include "../common/navbar.ftl">
    <!--菜单回显-->
    <#assign currentMenu="role">
    <#include "../common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>角色编辑</h1>
        </section>
        <section class="content">
            <div class="box">
                <form class="form-horizontal" action="/role/saveOrUpdate.do" method="post" id="editForm">
                <#--id隐藏域-->
                    <input type="hidden" value="${(role.id)!''}" name="id">
                <#--name-->
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-2 control-label">名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name" value="${(role.name)!''}"
                                   placeholder="请输入名称">
                        </div>
                    </div>
                <#--sn编码-->
                    <div class="form-group">
                        <label for="sn" class="col-sm-2 control-label">编码：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="sn" name="sn" value="${(role.sn)!''}"
                                   placeholder="请输入编码">
                        </div>
                    </div>

                <#--权限-->
                    <div class="form-group " id="role">
                        <label for="role" class="col-sm-2 control-label">分配权限：</label><br/>
                        <div class="row" style="margin-top: 10px">
                            <div class="col-sm-2 col-sm-offset-2">
                            <#--左边框-->
                                <select multiple class="form-control allPermissions" size="15">
                                    <#list permission as p>
                                        <option value="${p.id}">${p.name}</option>
                                    </#list>
                                </select>
                            </div>

                            <div class="col-sm-1" style="margin-top: 60px;" align="center">
                                <div>

                                    <a type="button" class="btn btn-primary  " style="margin-top: 10px" title="右移动"
                                       onclick="moveSelected('allPermissions', 'selfPermissions')">
                                        <span class="glyphicon glyphicon-menu-right"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="左移动"
                                       onclick="moveSelected('selfPermissions', 'allPermissions')">
                                        <span class="glyphicon glyphicon-menu-left"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全右移动"
                                       onclick="moveAll('allPermissions', 'selfPermissions')">
                                        <span class="glyphicon glyphicon-forward"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全左移动"
                                       onclick="moveAll('selfPermissions', 'allPermissions')">
                                        <span class="glyphicon glyphicon-backward"></span>
                                    </a>
                                </div>
                            </div>

                        <#--右边框-->
                            <div class="col-sm-2">
                                <select multiple class="form-control selfPermissions" size="15" name="ids">
                                <#-- 判断他们不为空 rele.和permissions 是两个条件,所以要用括号括起来 -->
                                <#--遍历,获取到我们查询出来的权限信息-->
                                    <#if (role.permission)??>
                                        <#list role.permission as p>
                                            <option value="${p.id}">${p.name}</option>
                                        <#-- <option value="${p.id}">${p.name}</option>-->
                                        </#list>
                                    </#if>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-6">
                            <button type="button" id="btn-submit" class="btn btn-primary">保存</button>
                            <button type="reset" class="btn btn-danger">重置</button>
                        </div>
                    </div>

                </form>

            </div>
        </section>
    </div>
    <#include "../common/footer.ftl">
</div>

</body>
</html>


<#--
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>角色管理</title>

    <#include "../common/link.ftl">

    <script>
        

        $(function () {
            //排重
            var ids = $.map($(".selfPermissions option"), function (item) {
                return item.value;
            })

            $.each($(".allPermissions option"), function (index, item) {
                if ($.inArray(item.value, ids) >= 0) {
                    $(item).remove();
                }
            });

            //提交表单
            $("#btn-submit").click(function () {
                $(".selfPermissions option").prop("selected", true);
                $("#editForm").submit();
            })
            var role = "";
            //超级管理员
            $("#admin").change(function () {
                if (this.checked) {
                    role = $("#role").detach();
                } else {
                    $(this).closest(".form-group").after(role);
                }
            })
            //编辑时候， 根据是否是超级管理员决定是否显示下拉框
            <#if (role.admin)?? &&  role.admin>
                role = $("#role").detach();
            </#if>
        })

        //移动选中
        function moveSelected(srcCls, targetCls) {
            $("." + srcCls + " option:selected").appendTo($("." + targetCls));
        }
        //移动全部
        function moveAll(srcCls, targetCls) {
            $("." + srcCls + " option").appendTo($("." + targetCls))
        }
    </script>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <#include "../common/navbar.ftl">
    <!--菜单回显&ndash;&gt;
    <#assign currentMenu="role">
    <#include "../common/menu.ftl">
    <div class="content-wrapper">
        <section class="content-header">
            <h1>角色编辑</h1>
        </section>
        <section class="content">
            <div class="box">
                <form class="form-horizontal" action="/role/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" value="${(role.id)!''}" name="id" >
                    <div class="form-group" style="margin-top: 10px;">
                        <label for="name" class="col-sm-2 control-label">名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name" value="${(role.name)!''}" placeholder="请输入名称">
                        </div>
                    </div>
                    &lt;#&ndash;密码为空:!role??    and    密码不为空role??&ndash;&gt;
                    <#if !role??>
                    <div class="form-group">
                        <label for="password" class="col-sm-2 control-label">编码：</label>
                        <div class="col-sm-6">
                            <input type="sn" class="form-control" id="sn" name="sn" placeholder="请输入编码">
                        </div>
                    </div>

                    <div class="form-group " id="role">
                        <label for="role" class="col-sm-2 control-label">分配权限：</label><br/>
                        <div class="row" style="margin-top: 10px">
                            <div class="col-sm-2 col-sm-offset-2">
                                <select multiple class="form-control allPermissions" size="15">
                                <#list permissions as p>
                                    <option value="${p.id}">${p.name}</option>
                                </#list>
                                </select>
                            </div>

                            <div class="col-sm-1" style="margin-top: 60px;" align="center">
                                <div>

                                    <a type="button" class="btn btn-primary  " style="margin-top: 10px" title="右移动"
                                       onclick="moveSelected('allPermissions', 'selfPermissions')">
                                        <span class="glyphicon glyphicon-menu-right"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="左移动"
                                       onclick="moveSelected('selfPermissions', 'allPermissions')">
                                        <span class="glyphicon glyphicon-menu-left"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全右移动"
                                       onclick="moveAll('allPermissions', 'selfPermissions')">
                                        <span class="glyphicon glyphicon-forward"></span>
                                    </a>
                                </div>
                                <div>
                                    <a type="button" class="btn btn-primary " style="margin-top: 10px" title="全左移动"
                                       onclick="moveAll('selfPermissions', 'allPermissions')">
                                        <span class="glyphicon glyphicon-backward"></span>
                                    </a>
                                </div>
                            </div>

                            <div class="col-sm-2">
                                <select multiple class="form-control selfPermissions" size="15" name="ids">
                                <#if (role.roles)??>
                                    <#list role.permissions as p>
                                        <option value="${p.id}">${p.name}</option>
                                    </#list>
                                </#if>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-1 col-sm-6">
                            <button id="btn-submit" type="button" class="btn btn-primary ">保存</button>
                            <button type="reset" class="btn btn-danger">重置</button>
                        </div>
                    </div>

                </form>

            </div>
        </section>
    </div>
    <#include "../common/footer.ftl">
</div>

</body>
</html>-->
